const api = require("../../../../utils/apiUtils");
const app = getApp();

Page({
  data: {
    id:null,
    info:{},
    imgLoadingList:[],
    tabCur:0,
    tabList:['产品说明','租赁说明','常见问题'],
    modalName:null,
    myLatitude:'',
    myLongitude:'',
    placeListData:[]
  },
  _loadInfo:function(){
    if(this.data.id){
        api.post(api.url.bxscnBatteryInfo,{
            batteryType:this.data.id,
        },res=>{
            if(res.data.code == 200){
                this.setData({
                    info:res.data.body,
                    imgLoadingList:res.data.body.images
                })
            }else if(res.data.msg){
                my.showModal({
                content: res.data.msg,
                showCancel:false,
                complete: (res) => {
                    my.navigateBack();
                }
                })
            }else {
                my.showToast({
                  content: '获取数据失败',
                  icon:'error'
                })
                setTimeout(() => {
                    my.navigateBack();
                }, 1000);
            }
        })
    }
  },

  _loadPlaceList(){
      let batteryType = this.data.info.id
      my.showLoading({
          title:'加载中...'
      })
      const _self = this;
      api.post(api.url.bxscnBatteryShop,{
          batteryType:batteryType,
          longitude:this.data.myLongitude,
          latitude:this.data.myLatitude
      },res=>{
          my.hideLoading();
          if(res.data.code==200){
              _self.setData({
                  placeListData:res.data.body.items,
              })
          }else if(res.data.code==202){
              my.showModal({
                  content: '暂无可预约租借的网点',
                  showCancel:false,
                  complete: (res) => {
                    _self.setData({
                        modalName:null
                    })
                  }
              })
          }else if(res.data.msg){
              my.showModal({
                  content: res.data.msg,
                  showCancel:false,
                  complete: (res) => {
                      _self.setData({
                          modalName:null
                      })
                  }
                })
          }else{
              _self.setData({
                  modalName:null
              })
              my.showToast({
                content: '获取数据失败',
                icon:'error'
              })
          }
      })
  },

  bindHideModal:function(e){
      this.setData({
          modalName:null
      })
  },

  bindImgLoad:function(e){
      let index = e.currentTarget.dataset.index;
      let imgLoadingList = this.data.imgLoadingList;
      imgLoadingList[index] = 100;
      this.setData({imgLoadingList});
  },

  bindTabChange:function(e){
      let index = e.currentTarget.dataset.index;
      this.setData({
          tabCur:index
      })
  },

  btnPlaceSelect:function(e){
      this.setData({
          modalName:'placeSelect'
      })
      this._loadPlaceList();
  },

  toPlaceLease:function(e){
      this.setData({
          modalName:null
      })
      let index = e.currentTarget.dataset.index;
      let place = this.data.placeListData[index];
      const _info = this.data.info;
      my.navigateTo({
          url: '../lease/lease',
          events: {
              fromData:function(data){}
          },
          success: function(res) {
              res.eventChannel.emit('fromData',{
                  info:_info,
                  place:place
              })
          }
      })
  },
  onLoad(options) {
    this.setData({
      id:options.id,
      myLatitude:options.latitude,
      myLongitude:options.longitude
  })
  this._loadInfo();
  },
});
